﻿@using System.Text
@using Abp.Collections.Extensions
@using Partner.Surround.Admin.Views
@using Partner.Surround.Admin.Views.Shared.Components.SideBarMenu
@using System.Security.Cryptography
@model SideBarMenuViewModel
@{
    var calculateMenuUrl = new Func<string, string>((url) =>
    {
        if (string.IsNullOrEmpty(url))
        {
            return ApplicationPath;
        }

        if (UrlChecker.IsRooted(url))
        {
            return url;
        }

        return ApplicationPath + url;
    });

    var calculateMenuId = new Func<string, string, string>((Name, Url) =>
    {
        using (var md5 = MD5.Create())
        {
            var hashTarget = string.IsNullOrEmpty(Url) ? Name : Url;
            var md5ComputeHash = md5.ComputeHash(Encoding.UTF8.GetBytes(hashTarget));
            var bitConverterResult = BitConverter.ToString(md5ComputeHash);
            var replaceResult = bitConverterResult.Replace("-", "");
            return replaceResult;
        }
    });

    var orderedMenuItems = Model.MainMenu.Items.Where(x => x.IsVisible).ToList();
}

<ul class="mui-table-view mui-grid-view mui-grid-9">
    @foreach (var menuItem in orderedMenuItems)
    {
        @if (menuItem.Items.IsNullOrEmpty())
        {
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <a href="@menuItem.Url">
                    <span class="mui-icon mui-icon-home"></span>
                    <div class="mui-media-body">@menuItem.DisplayName</div>
                </a>
            </li>
        }
        else
        {
            @foreach (var subMenuItem in menuItem.Items)
            {
                @if (subMenuItem.Items.IsNullOrEmpty())
                {
                    <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                        <a href="@subMenuItem.Url">
                            <span class="mui-icon mui-icon-home"></span>
                            <div class="mui-media-body">@subMenuItem.DisplayName</div>
                        </a>
                    </li>
                }
                else
                {
                    @foreach (var subSubMenuItem in subMenuItem.Items)
                    {
                        <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                            <a href="@subSubMenuItem.Url">
                                <span class="mui-icon mui-icon-home"></span>
                                <div class="mui-media-body">@subSubMenuItem.DisplayName</div>
                            </a>
                        </li>
                    }
                }
            }
        }
    }
</ul>
